package com.zmn.plat.services.impl.product.tariff.tariffproductitem3;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto.KvDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.common.dto.price.ItemTo;
import com.zmn.plat.common.dto.price.meter3.ItemDRO3;
import com.zmn.plat.model.entity.product.tariff.item.ProductItemQuery;
import com.zmn.plat.model.entity.product.tariff.item.ProductTariffGroupItemQuery;
import com.zmn.plat.model.entity.product.tariff.tariffproductitem3.TariffProductItem3;
import com.zmn.plat.model.entity.product.tariff.tariffproductitem3.TariffProductItem3Query;
import com.zmn.plat.persistence.interfaces.product.tariff.tariffproductitem3.TariffProductItem3Dao;
import com.zmn.plat.services.interfaces.product.tariff.tariffproductitem3.TariffProductItem3Service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.zmn.common.constant.GlobalConsts;

/**
 * 描述: 价格表-产品-项目-计价器3.0接口实现类
 *
 * @author guoqiao
 * @since 2021/11/19 15:28
 */
@Slf4j
@Service
public class TariffProductItem3ServiceImpl implements TariffProductItem3Service {

    private static final String TAG = "价格表-产品-项目-计价器3.0基础服务接口";

    @Resource
    private TariffProductItem3Dao tariffProductItem3Dao;
    @Resource
    RedisTemplate redisTemplate;

    @Override
    public TariffProductItem3 findById(Integer relateId) {
        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id查询数据relateId为null", TAG);
            return null;
        }
        return tariffProductItem3Dao.findById(relateId);
    }

    @Override
    public List<TariffProductItem3> listByIds(List<Integer> relateIds) {
        if (CollectionUtils.isEmpty(relateIds)) {
            log.error("[{}],按条件查询数据,relateIds为null", TAG);
            return null;
        }
        return tariffProductItem3Dao.listByIds(relateIds);
    }

    @Override
    public List<TariffProductItem3> listByQuery(TariffProductItem3Query query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return tariffProductItem3Dao.listByQuery(query);
    }

    @Override
    public List<ItemDRO3> listItemPriceByQuery(ProductItemQuery query) {
        String key = "plat:redis10m:" + JSON.toJSONString(query);
        List<ItemDRO3> itemDRO3s = (List<ItemDRO3>) redisTemplate.opsForValue().get(key);
        if (CollectionUtils.isEmpty(itemDRO3s)) {
            itemDRO3s = tariffProductItem3Dao.listItemPriceByQuery(query);
            redisTemplate.opsForValue().set(key, itemDRO3s, 10, TimeUnit.MINUTES);
            return itemDRO3s;
        }
        return itemDRO3s;
    }

    @Override
    @Cacheable(cacheNames = "redis10m", key = "'price3:countByQuery:ProductTariffGroupItemQuery:'+#p0", unless = "#result == null")
    public Integer countByQuery(ProductTariffGroupItemQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return tariffProductItem3Dao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(TariffProductItem3 tariffProductItem3) {
        if (Objects.isNull(tariffProductItem3.getRelateId())) {
            log.error("[{}],根据id修改数据,tariffProductItem3为null", TAG);
            return null;
        }
        return tariffProductItem3Dao.updateByKey(tariffProductItem3);
    }

    @Override
    public void updateStatusBatch(List<Integer> ids, Integer status, String operator) {
        if (!Objects.equals(status, GlobalConsts.NO) && !Objects.equals(status, GlobalConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        tariffProductItem3Dao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert(TariffProductItem3 tariffProductItem3) {
        if (Objects.isNull(tariffProductItem3)) {
            log.error("[{}],新增数据,tariffProductItem3为null", TAG);
            return null;
        }
        return tariffProductItem3Dao.insert(tariffProductItem3);
    }

    @Override
    public void insertBatch(List<TariffProductItem3> list) {
        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        tariffProductItem3Dao.insertBatch(list);
    }

    @Override
    public Integer deleteByKey(Integer relateId) {
        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id删除数据,relateId为null", TAG);
            return null;
        }
        return tariffProductItem3Dao.deleteByKey(relateId);
    }

}